package com.dougmccune.charts.gantt
{
	import com.dougmccune.controls.HSlider;
	import com.dougmccune.controls.sliderClasses.SliderThumb;
	
	import flash.display.DisplayObject;
	import flash.display.Graphics;
	import flash.events.FocusEvent;
	
	import mx.core.IFlexDisplayObject;
	import mx.core.mx_internal;
	import mx.styles.ISimpleStyleClient;
	import mx.formatters.NumberFormatter;
	
	use namespace mx_internal;
	
	[ExcludeClass]
	
	[Style(name="incompleteSkin", type="Class", inherit="no")]
	[Style(name="completeSkin", type="Class", inherit="no")]

	[Style(name="incompleteColor", type="Color", inherit="no")]
	[Style(name="completeColor", type="Color", inherit="no")]

	
	public class GanttSlider extends com.dougmccune.controls.HSlider
	{
		
		private var _myHeight:Number = 0;
		
		protected var incompleteTrack:IFlexDisplayObject;
		
		
		public function GanttSlider()
		{
			super();
			
			this.dataTipFormatFunction = formatDataTip;
		}
		
		override protected function createThumbs():void {
			super.createThumbs();
			
			var n:int = this.thumbCount;
	        for (var i:int = 0; i < n; i++)
	        {
	            var thumb:SliderThumb = this.getThumbAt(i);
	

				thumb.focusEnabled = false;
				
	            thumb.removeEventListener(FocusEvent.FOCUS_IN,
	                                   thumb_focusInHandler);
	            thumb.removeEventListener(FocusEvent.FOCUS_OUT,
	                                   thumb_focusOutHandler);
	        }
		}
		
		private function formatDataTip(value:Number, thumb:GanttSliderThumb):String { 
           if(this.getThumbAt(1) == thumb) {
           		var pct:Number = (value - this.values[0])/(this.values[2] - this.values[0]); 
           		return Math.round(pct * 100) + "%";
           }
           else {
           		var f:NumberFormatter = new NumberFormatter();
           		f.precision = 2;
           		return f.format(value);
           }
       }
		
		
		
		/**
     *  @private
     */
    override protected function thumb_focusInHandler(event:FocusEvent):void
    {
        event.stopImmediatePropagation();
        //dispatchEvent(event);
    }

    /**
     *  @private
     */
    override protected function thumb_focusOutHandler(event:FocusEvent):void
    {
        event.stopImmediatePropagation();
        //dispatchEvent(event);
    
    }
		
		override public function styleChanged(styleProp:String):void
	    {
	        var anyStyle:Boolean = styleProp == null || styleProp == "styleName";
	        
	        super.styleChanged(styleProp);

			if (styleProp == "incompleteSkin" || anyStyle)
	        {
	            if (innerSlider && incompleteTrack)
	            {
	                innerSlider.removeChild(DisplayObject(incompleteTrack));
	                incompleteTrack = null;
	            }
	            trackHighlightChanged = true;
	            invalidateProperties();
	        }
	    }
	    

		override protected function commitProperties():void
    	{
        	
			if (trackHighlightChanged)
	        {
	            trackHighlightChanged = false;
	
	            if (getStyle("showTrackHighlight"))
	            {
	                createHighlightTrack();
	            }
	            else if (highlightTrack)
	            {
	                innerSlider.removeChild(DisplayObject(highlightTrack));
	                highlightTrack = null;
	                
	                innerSlider.removeChild(DisplayObject(incompleteTrack));
	                incompleteTrack = null;
	            }
	        }
	        
	        super.commitProperties();
		
    	}
    	
    	
    	override protected function createHighlightTrack():void
		{
			super.createHighlightTrack();
			
			var showTrackHighlight:Boolean = getStyle("showTrackHighlight");
			if (!incompleteTrack && showTrackHighlight)
			{
				var incompleteClass:Class =
					getStyle("incompleteSkin");
				incompleteTrack = new incompleteClass();

				if (incompleteTrack is ISimpleStyleClient)
					ISimpleStyleClient(incompleteTrack).styleName = this;
	
				innerSlider.addChildAt(DisplayObject(incompleteTrack),
					innerSlider.getChildIndex(DisplayObject(track)) + 2);
			}
		}
    	
    	override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void {
    		super.updateDisplayList(unscaledWidth, unscaledHeight);
    		
    		
    		var n:int = this.thumbCount;
    		
    		for (var i:int = 0; i < n; i++)
	        {
	            var currentThumb:SliderThumb = SliderThumb(this.getThumbAt(i));
	            currentThumb.move(currentThumb.x, unscaledHeight - currentThumb.height);
	            currentThumb.visible = true;
	            currentThumb.setActualSize(currentThumb.getExplicitOrMeasuredWidth(),
	                                 currentThumb.getExplicitOrMeasuredHeight());
	        }
	       
    	}
    	
    	override mx_internal function drawTrackHighlight():void
		{
			
			if (highlightTrack)
			{
				var xPos:Number;
				var completeWidth:Number;
				var incompleteWidth:Number;
		
	
				var firstThumb:SliderThumb = SliderThumb(this.getThumbAt(0));
				var lastThumb:SliderThumb = firstThumb;
				
				var midThumb:SliderThumb = this.getThumbAt(1);
				
				for(var i:int=0; i<this.thumbCount; i++) {
					var curThumb:SliderThumb = SliderThumb(this.getThumbAt(i));
					
					if(curThumb.xPosition < firstThumb.xPosition) {
						firstThumb = curThumb;
					}
					
					if(curThumb.xPosition > lastThumb.xPosition) {
						lastThumb = curThumb;
					}
				}
				
				
				xPos = firstThumb.xPosition;
				completeWidth = midThumb.xPosition - firstThumb.xPosition;
				incompleteWidth = lastThumb.xPosition - midThumb.xPosition;
				
	
	
				
				var fullThumbHeight:Number = firstThumb.getExplicitOrMeasuredHeight();
				var halfThumbHeight:Number = (!fullThumbHeight) ? 0 : (fullThumbHeight / 2);
			
			
				var highlightSkinHeight:Number = this.height;
			
				
				
				highlightTrack.visible = completeWidth > 0;
				highlightTrack.setActualSize(completeWidth > 0 ? completeWidth : 0, highlightSkinHeight);
				highlightTrack.move(xPos, 0);
				
				incompleteTrack.visible = incompleteWidth > 0;
				incompleteTrack.setActualSize(incompleteWidth > 0 ? incompleteWidth : 0, highlightSkinHeight);
				incompleteTrack.move(midThumb.xPosition, 0);
				
			
				var g:Graphics = highlightHitArea.graphics;
	
				
				g.clear();		
				g.beginFill(0xff0000,0);
				
				g.drawRect(xPos, 
						0, 
						completeWidth + incompleteWidth > 0 ? completeWidth + incompleteWidth : 0, 
						this.height);
					
				
					
				g.endFill();
			}
		}
		
		
		
		
		
	}
}